package com.example.itcacp.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring Boot 3 中，@ServerEndpoint 注解的 WebSocket 端点由 Java WebSocket API（JSR-356）管理，而不是 Spring 容器直接管理，因此默认情况下无法通过 Spring 自动注入依赖
 * 工具类，获取 Spring 上下文中的 Bean
 * */

@Component
public class SpringContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> beanClass) {
        return applicationContext.getBean(beanClass);
    }
}
